﻿package com.dLibs.photos
{	
	import com.dLibs.events.MouseEvents;
	import com.dLibs.utils.scaleObject;
	
	import flash.display.*;
	import flash.events.*;
	import flash.geom.Rectangle;
	import flash.net.*;
	import flash.system.System;
	import flash.utils.*;
	
	import com.greensock.*;
	import com.greensock.easing.*;
	/**
	 * 
	 * @author Pablo Davi
	 * 
	 */	
	public class gallLoader extends MovieClip
	{
		/* @ Variables	 				____________________________________________________*/
		
		private var _minData		:Array;
		private var _maxData		:Array;
		private var _thmBox			:Array;
		private var _bigPict		:MovieClip;
		private var _smallPict		:MovieClip;
		private var _thmClip		:MovieClip;
		private var _thmMovie		:MovieClip;
		private var _bigMovie		:MovieClip;
		private var _loaderClip		:MovieClip;
		private var _thumbClass		:String;
		private var _loaderThumb	:Loader;
		private var _loaderBig		:Loader;
		private var _i				:uint;
		private var _thumbPadding	:uint;
		private var _thumbLines		:uint;
		private var _thumbLineY		:uint;
		private var _thumbLineX		:uint;
		private var _thumbMargin	:uint;
		private var _wScale			:Number;
		private var _hScale			:Number;
		private var _moreScale		:Number;
		private var _imageZoom		:Boolean;
		private var _imgWidth		:Number;
		private var _imgHeight		:Number;
		
		private var _moreZoom		:MovieClip;
		private var _lessZoom		:MovieClip;
		private var maxWidth		:Number;
		private var maxHeight		:Number;
		private var minWidth		:Number;
		private var minHeight		:Number;
		private var growImage		:Number = 0.2;
		private var iGrow			:uint;
		private var _dragObject		:MovieClip;
		private var clips			:Array;
		private var listPhotos		:Array;
		private var listBigPhotos	:Array;
		
		private var plus_i			:uint;
		
		/* @ Constructor 				____________________________________________________*/
		
		public function gallLoader():void
		{			
			listBigPhotos = new Array();
		};
		
		public function addImages(minData:Array,maxData:Array):void
		{
			_minData 		= minData;
			_maxData 		= maxData;
			// trace(_minData);
		}
		
		public function addStructure(bigPicture:MovieClip,smallPictures:MovieClip,thumbClass:String):void
		{
			_bigPict 		= bigPicture;
			_smallPict 		= smallPictures;
			_thumbClass 	= thumbClass;
		}
		
		public function addConfig(thumbMargin:uint = 5, thumbPadding:uint=0, thumbLines:uint=1, moreScale:Number=1):void
		{
			_thumbMargin	= thumbMargin
			_thumbPadding 	= thumbPadding;
			_thumbLines		= thumbLines;
			_moreScale		= moreScale;
		}
		
		public function addZoom(imageZoom:Boolean = false, zoomButtons:Object = null ):void
		{
			_imageZoom		= imageZoom;			
			
			if ( _imageZoom ) 
			{
				_moreZoom 	= zoomButtons.moreZoom;
				_lessZoom 	= zoomButtons.lessZoom;
			}
		}
		
		public function addLoader(loaderClip:MovieClip = null):void
		{
			_loaderClip = loaderClip;
		}
		
		
		public function init():void
		{
			listPhotos = new Array();
			if ( _loaderClip != null ) _loaderClip.visible = false;
			
			clips 		= new Array();
			_thmBox 	= new Array();
			
			for ( _i = 0; _i < _minData.length; _i++ )
			{
				_loaderThumb 	= new Loader();
				
				_loaderThumb.x 	= _thumbPadding;
				_loaderThumb.y 	= _thumbPadding;
				_loaderThumb.load(new URLRequest(_minData[_i]));
				// trace(_minData[_i]);
				_loaderThumb.contentLoaderInfo.addEventListener(Event.COMPLETE, showThumb);
								
				_thmClip 	= new ( getDefinitionByName(_thumbClass) as Class )();
				_thmClip.addChild(_loaderThumb);
				
				_wScale = (_thmClip.width - _thumbPadding*2) * _moreScale; 
				_hScale = (_thmClip.height - _thumbPadding*2) * _moreScale;
				
				_thmMovie 		= new MovieClip();
				// clips.[_i] 		= _thmMovie;
				
				_thmMovie.x 	= ( _thmClip.width + _thumbMargin ) * _thumbLineX;
				_thmMovie.y		= ( _thmClip.height + _thumbMargin ) * _thumbLineY;				
				_thmMovie.id 	= _i;
				_thmMovie.buttonMode = true;
				
				trace(_thmMovie.x, _thmMovie.y);
				
				_thmMovie.addEventListener(MouseEvent.CLICK, viewPicture);
				_thmBox.push(_thmMovie);
				_thmMovie.addChild(_thmClip);
				
				_smallPict.addChild(_thmMovie);
				
				listPhotos[_i] = ({um:_thmMovie, dois:_thmClip, tres:_loaderThumb});
				
				if ( _thumbLineX < 1000 )
				{
					_thumbLineX++;
				}
				else 
				{
					_thumbLineX = 0;
					_thumbLineY++;
				}				
				
			};
			_thmBox[0].dispatchEvent(new MouseEvent(MouseEvent.CLICK));
			this.addEventListener(Event.REMOVED_FROM_STAGE, destroy);
		};
		
		public function get jrs_BigLoader():Loader
		{
			return _loaderBig;
		}
		public function get jrs_ArrClips():Array
		{
			return clips;
		}
		public function get jrs_ArrListPhotos():Array
		{
			return listPhotos;
		}
		private function allBlackWhite():void
		{
			var ma:uint = _thmBox.length;
			for ( var iii:uint = 0; iii < ma; iii++ )
			{
				TweenMax.to(_thmBox[iii], .3, {colorMatrixFilter:{ saturation : 0 }});
			}
		}
		
		private function showThumb(e:Event):void
		{
			var so:scaleObject = new scaleObject(e.currentTarget.content,{width:_wScale,height:_hScale});
		}
		
		private function viewPicture(e:MouseEvent):void
		{
			if( _loaderBig != null ) 
			{
				_loaderBig.unload();
				System.gc();
			}
			
			_loaderBig = new Loader();
			
			_loaderBig.load(new URLRequest(_maxData[e.currentTarget.id]));
			_loaderBig.contentLoaderInfo.addEventListener(Event.COMPLETE, showLoadedBig);
			_loaderBig.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, showLoaderBig);
			allBlackWhite();
			TweenMax.to(e.currentTarget, .7, {colorMatrixFilter:{ saturation : 1 }});		
			
			while ( _bigPict.numChildren != 0 ) 
			{
				_bigPict.removeChildAt(0);
			}			
			dispatchEvent(new MouseEvents(MouseEvents.CLICK, { id : e.currentTarget.id }));
		};
		
		private function showLoaderBig(e:ProgressEvent):void
		{
			if ( _loaderClip != null ) _loaderClip.visible = true;
		};
		
		private function showLoadedBig(e:Event):void
		{
			if ( _loaderClip != null ) _loaderClip.visible = false;
			
			var bmp:Bitmap = e.currentTarget.content;
				bmp.smoothing = true;
			
			_bigMovie 		= new MovieClip();
			_bigMovie.alpha = 0;
			_bigMovie.addChild(_loaderBig);
			_bigPict.addChild(_bigMovie);
			_bigPict.mask 	= _dragObject;
			_bigPict.scaleX = 1;
			_bigPict.scaleY = 1;
			
			if ( _imageZoom )
			{
				_bigPict.x = _dragObject.x;
				_bigPict.y = _dragObject.y;
				
				maxWidth 				= _bigPict.width;
				maxHeight				= _bigPict.height;
				
				_imgWidth				= _dragObject.width;
				_imgHeight				= _dragObject.height;
				
				var iLoaded:scaleObject = new scaleObject(e.currentTarget.content,{ width:_imgWidth, height:_imgHeight });
				
				_moreZoom.buttonMode 	= true;
				_lessZoom.buttonMode 	= true;   
				
				minWidth 				= _bigPict.width;
				minHeight				= _bigPict.height;
				
				_moreZoom.addEventListener(MouseEvent.CLICK, ImageplusZoom);
				_lessZoom.addEventListener(MouseEvent.CLICK, ImagelessZoom);
				
				_bigPict.addEventListener(MouseEvent.MOUSE_DOWN, initDrag);
				_bigPict.stage.addEventListener(MouseEvent.MOUSE_UP, stopDraging);
			}
			listBigPhotos.push({um:_bigMovie, dois:_loaderBig});//_bigPict
			TweenLite.to(_bigMovie,.4,{ alpha : 1 });
		};
		
		public function set setDragObject(dragObject:MovieClip):void
		{
			if ( _imageZoom )
			{
				_dragObject = dragObject;
			}
		}
		
		private function initDrag(event:Event):void 
		{
			var _x:Number = _bigPict.width 	- 	_dragObject.width;
			var _y:Number = _bigPict.height - 	_dragObject.height;
			
			if ( _x < 0 ) _x = 0;
			if ( _y < 0 ) _y = 0;
			
			var rec:Rectangle = new Rectangle(_dragObject.x - _x, _dragObject.y - _y ,_x, _y);
			_bigPict.startDrag(false,rec);
		}
		
		private function ImageplusZoom(m:MouseEvent):void
		{
			if ( plus_i < 5 )
			{
				_bigPict.scaleX = _bigPict.scaleY += 0.2;
				plus_i++;
			}			
		}
		
		private function ImagelessZoom(m:MouseEvent):void
		{
			if ( plus_i > 0 )
			{
				_bigPict.scaleX = _bigPict.scaleY -= 0.2;
				verifyZoom();
				plus_i--;
			}
		}
		
		private function verifyZoom():void
		{
			if ( _bigPict.width < _dragObject.width || _bigPict.height < _dragObject.height )
			{
				_bigPict.scaleX = _bigPict.scaleY += 0.2;
			}
			if ( (_bigPict.x + _bigPict.width) < ( _dragObject.x + _dragObject.width ) ) _bigPict.x = (_dragObject.x + _dragObject.width) - _bigPict.width; 
			if ( (_bigPict.y + _bigPict.height) < ( _dragObject.y + _dragObject.height ) ) _bigPict.y = (_dragObject.y + _dragObject.height) - _bigPict.height;
		}		
		
		private function stopDraging(event:MouseEvent):void 
		{
			_bigPict.stopDrag();
		}
		
		public function destroy(event:Event):void
		{
			for ( var ai:uint = 0; ai < listPhotos.length; ai++ )
			{
				(listPhotos[ai].tres as Loader).unload();
				(listPhotos[ai].dois as MovieClip).removeChild(listPhotos[ai].tres);
				// _thmMovie.removeChild(listPhotos[ai].dois);
				_smallPict.removeChild(listPhotos[ai].um);
				System.gc();
			}
			
			if ( _imageZoom )
			{
				_moreZoom.removeEventListener(MouseEvent.CLICK, ImageplusZoom);
				_lessZoom.removeEventListener(MouseEvent.CLICK, ImagelessZoom);
			}
				
			_bigPict.removeEventListener(MouseEvent.MOUSE_DOWN, initDrag);
			_bigPict.stage.removeEventListener(MouseEvent.MOUSE_UP, stopDraging);
			
			System.gc();
		}
				
	};
};